package com.liu.activemq.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

public class JmsConsumer_Topic_Persist {

    public static final String ACTIVEMQ_URL = "tcp://192.168.25.133:61616";
    public static final String TOPIC_NAME = "topic01";


    public static void main(String[] args) throws JMSException, IOException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setClientID("z3");
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
        TopicSubscriber durableSubscriber = session.createDurableSubscriber(topic, "remark...");
        /**
         * 与queue不同的是，持久化的topic的start操作在下面
         * 在start启动连接之前，我们需要配置订阅者topic的id
         * 接着再去获取TopicSubscriber订阅者对象，除了填入Destination对象，还要将
         * 备注信息填入，但可以任意取备注信息,其他步骤不变
         */
        connection.start();

        Message receive = durableSubscriber.receive();
        while (receive != null) {
            TextMessage text = (TextMessage) receive;
            System.out.println(text.getText());
            receive = durableSubscriber.receive(5000l);
        }
        durableSubscriber.close();
        session.close();
        connection.close();
    }
}
